﻿/*
 * Copyright (C) 2012, Jesus Requejo <jesus.requejo@gmail.com> 
 * Licensed under Microsoft Public License (Ms-PL)
 * http://www.microsoft.com/en-us/openness/licenses.aspx#MPL
*/

namespace Infrastructure {

   using System;
   using System.Collections.Generic;
   using System.Runtime.InteropServices;
   
   public class Win32WindowMessengerWrapper {
      [DllImport("user32")]
      private static extern bool PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam);
      [DllImport("user32")]
      private static extern int RegisterWindowMessage(string message);
      [DllImport("User32.dll")]
      private static extern Int32 FindWindow(String lpClassName, String lpWindowName);

      static IDictionary<string, int> messageNamesAndIds = new Dictionary<string, int>();

      public static void SendMessageToWindow(string windowName, string message) {
         
         int WM_MESSAGE = RegisterWindowMessage(message);

         int h = FindWindow(null, windowName);
         IntPtr windowHandle = new IntPtr(h);
         PostMessage(
               windowHandle,
               WM_MESSAGE,
               IntPtr.Zero,
               IntPtr.Zero);
      }

      public static int GetMessageId(string message) {
         int WM_MESSAGE = RegisterWindowMessage(message);
         return WM_MESSAGE;
      }
   }
}
